package com.webserver.core;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.net.URISyntaxException;

public class DispatcherServlet {
    private static File rootDir;
    private static File staticDir;

    static{
        try {
        rootDir=new File(
                    ClientHandler.class.getClassLoader()
                            .getResource(".").toURI()
            );
            staticDir = new File(rootDir,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public void service(HttpServletRequest request, HttpServletResponse response){
        String path=request.getUri();


        File file=new File(staticDir,path);
        System.out.println("资源是否存在:"+file.exists());

        if(file.isFile()){
            response.setContentFile(file);


        }else {
            file = new File(staticDir, "/root/404.html");
            response.setStatusCode(404);
            response.setStatusReason("NotFound");
            response.setContentFile(file);


        }
    }
}
